/*
 * Canvas Game Library
 * Module: Audio
 * 
 * Copyright Chainsawrus Wreaks 2011
 * All rights reserved.
 */

/**
 * @fileOverview This is the Audio class for CGL, here there will be methods for loading and playing sounds
 * as well as global settings for volume and mute, you will also be able to check the audio capability off the
 * client on the fly to be able to deliver your game to as many plattforms and browsers as possible!
 */

/**
 * AJAX module namespace
 * @namespace CGL.audio
 */

CGL.audio = {
	MUSIC_ACTIVE : true,
	SOUND_ACTIVE : true,
	MUSIC_VOLUME : 1.0,
	SOUND_VOLUME : 1.0,
	CAN_PLAY_MP3 : false,
	CAN_PLAY_OGG : false,
	CAN_PLAY_AUDIO : true,
	
	checkCapabilities : function() {
		var testAudio = new Audio();
		if(testAudio.canPlayType)
		{
			CGL.audio.CAN_PLAY_MP3 = !!testAudio.canPlayType && "" != testAudio.canPlayType('audio/mpeg');
		    CGL.audio.CAN_PLAY_OGG = !!testAudio.canPlayType && "" != testAudio.canPlayType('audio/ogg; codecs="vorbis"');
		}
		else
		{
			CGL.audio.CAN_PLAY_AUDIO = false;
		}
		CGL.util.printLn("MP3 Capabilities: " + CGL.audio.CAN_PLAY_MP3 + " OGG Capabilities: " + CGL.audio.CAN_PLAY_OGG);
	},
		
	Sample : function() {
		var sample = {
			audio : new Audio(),
			type : 0,
			
			load : function(src) {
				if(CGL.audio.CAN_PLAY_AUDIO)
				{
					if(CGL.audio.CAN_PLAY_MP3)
					{
						sample.audio.src = src + ".mp3";
					}
					else
					{
						sample.audio.src = src + ".ogg";
					}
				}
				sample.audio.onload = CGL.resources.notifyLoadedAudio();
				CGL.resources.addAudioResource();
			},
				
			play : function() {
				if(CGL.audio.SOUND_ACTIVE)
				{
					sample.audio.play();
				}
			},
				
			pause : function() {
				sample.audio.pause();
			},
				
			setLoop : function(loop) {
				sample.audio.loop = loop;
			},
				
			getLoop : function() {
				return sample.audio.loop;
			},
				
			replay : function() {
				sample.audio.currentTime = sample.audio.startTime;
				sample.play();
			}
		};
		return sample;
	}
};